package com.gzyz.cheetah.controller;


import com.gzyz.cheetah.annotations.AdminJwtIntercept;
import com.gzyz.cheetah.annotations.DownloadIntercept;
import com.gzyz.cheetah.annotations.UserJwtIntercept;
import com.gzyz.cheetah.entity.dto.DocumentDTO;
import com.gzyz.cheetah.entity.dto.ListIdDTO;
import com.gzyz.cheetah.entity.dto.PageDTO;
import com.gzyz.cheetah.entity.pagerequest.DocumentPageRequest;
import com.gzyz.cheetah.entity.table.Document;
import com.gzyz.cheetah.service.IDocumentService;
import com.gzyz.cheetah.util.Result;
import com.gzyz.cheetah.util.ThreadLocalUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * <p>
 * 文档表 前端控制器
 * </p>
 *
 * @author xclhove
 * @since 2023-11-06
 */
@RestController
@RequestMapping("/document")
@ApiOperation("文档")
@RequiredArgsConstructor
@Slf4j
public class DocumentController {
    private final IDocumentService documentService;
    
    @PostMapping("/upload")
    @ApiOperation("上传文档")
    @UserJwtIntercept
    public Result<Document> uploadDocument(@RequestBody MultipartFile documentFile) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        Document document = documentService.uploadDocument(userId, documentFile);
        return Result.success(document);
    }
    
    @DeleteMapping("/delete/{documentId}")
    @ApiOperation("通过文档id删除文档")
    @UserJwtIntercept
    public Result<Object> deleteDocumentById(@PathVariable Integer documentId) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        documentService.deleteDocumentById(userId, documentId);
        return Result.success();
    }
    
    @PostMapping("/delete")
    @ApiOperation("通过文档id批量删除文档")
    @UserJwtIntercept
    public Result<Object> deleteDocumentById(@RequestBody ListIdDTO listIdDTO) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        documentService.deleteDocumentsByIds(userId, listIdDTO.getIds());
        return Result.success();
    }
    
    @PostMapping("/update")
    @ApiOperation("更新文档")
    @UserJwtIntercept
    public Result<Object> updateDocument(@RequestBody Document document) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        document.setUserId(userId);
        documentService.updateDocument(document);
        return Result.success();
    }
    
    @GetMapping("/get/{documentId}")
    @ApiOperation("通过文档id获取一篇文档")
    @UserJwtIntercept
    public Result<Object> getOneDocumentById(@PathVariable Integer documentId) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        Document document = documentService.getOneDocumentById(userId, documentId);
        return Result.success(document);
    }
    
    @PostMapping("/search")
    @ApiOperation("分页搜索文档")
    @UserJwtIntercept
    public Result<PageDTO<DocumentDTO>> searchDocuments(@RequestBody DocumentPageRequest documentPageRequest) {
        PageDTO<DocumentDTO> documents = documentService.searchDocuments(documentPageRequest);
        return Result.success(documents);
    }
    
    @PostMapping("/get/user/{userId}")
    @ApiOperation("分页获取用户文档")
    @UserJwtIntercept
    public Result<PageDTO<Document>> pageUserDocuments(@PathVariable Integer userId,
                                                       @RequestBody DocumentPageRequest documentPageRequest
                                                       ) {
        Integer userIdInToken = (Integer) ThreadLocalUtil.get("id");
        boolean showPrivate = Objects.equals(userIdInToken, userId);
        PageDTO<Document> userDocuments = documentService.pageUserDocuments(userId, documentPageRequest, showPrivate);
        return Result.success(userDocuments);
    }
    
    @PostMapping("/freeze/{documentId}")
    @ApiOperation("通过文档id冻结文档")
    @AdminJwtIntercept
    public Result<Object> freezeDocumentById(@PathVariable Integer documentId) {
        documentService.freezeDocumentById(documentId);
        return Result.success();
    }
    
    @PostMapping("/unfreeze/{documentId}")
    @ApiOperation("通过文档id解冻文档")
    @AdminJwtIntercept
    public Result<Object> unFreezeDocumentById(@PathVariable Integer documentId) {
        documentService.unFreezeDocumentById(documentId);
        return Result.success();
    }
    
    @PostMapping("/freeze/user/{userId}")
    @ApiOperation("通过用户id批量冻结用户的文档")
    @AdminJwtIntercept
    public Result<Object> freezeUserDocuments(@PathVariable Integer userId) {
        documentService.freezeUserDocuments(userId);
        return Result.success();
    }
    
    @PostMapping("/unfreeze/user/{userId}")
    @ApiOperation("通过用户id批量解冻用户的文档")
    @AdminJwtIntercept
    public Result<Object> unFreezeUserDocuments(@PathVariable Integer userId) {
        documentService.unFreezeUserDocuments(userId);
        return Result.success();
    }
    
    @GetMapping("/download/{documentId}")
    @ApiOperation("通过文档id下载文档")
    @DownloadIntercept
    public void download(HttpServletResponse response, @PathVariable Integer documentId) {
        Integer userId = (Integer) ThreadLocalUtil.get("id");
        documentService.download(response, userId, documentId);
    }
    
    @GetMapping("/list")
    @AdminJwtIntercept
    public Result<PageDTO<Document>> list(@RequestParam Integer pageNumber,
                                          @RequestParam Integer pageSize,
                                          @RequestParam String searchTitle) {
        PageDTO<Document> documentPageDTO = documentService.pageDocuments(searchTitle, pageNumber, pageSize);
        return Result.success(documentPageDTO);
    }
    
    @PostMapping("/changecommentstatus/{documentId}")
    @AdminJwtIntercept
    public Result<Object> changeCommentStatus(@PathVariable Integer documentId) {
        documentService.changeCommentStatus(documentId);
        return Result.success();
    }
}
